<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/03/23 01:49:40 $)
// $RCSfile: functions_filters.php,v $ : $Revision: 1.2 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

/**
* Rebuilds the filter cache
*/
function rebuildFilterCache()
{
	global $dbCore, $_SWIFT, $datastore;

	$_cache = $_ticketfilteridlist = array();
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."ticketfilters` ORDER BY `title` ASC;");
	while ($dbCore->nextRecord())
	{
		$_cache[$dbCore->Record["ticketfilterid"]] = $dbCore->Record;
		$_cache[$dbCore->Record["ticketfilterid"]]["filterfields"] = array();

		$_ticketfilteridlist[] = $dbCore->Record["ticketfilterid"];
	}

	if (count($_ticketfilteridlist))
	{
		$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."ticketfilterfields` WHERE `ticketfilterid` IN (". buildIN($_ticketfilteridlist) .") ORDER BY `ticketfilterid` ASC;");
		while ($dbCore->nextRecord())
		{
			$_cache[$dbCore->Record["ticketfilterid"]]["filterfields"][] = $dbCore->Record;
		}
	}

	$datastore->update("ticketfiltercache", $_cache);

	return true;
}

/**
* Delete given filters
*/
function deleteTicketFilters($ticketfilteridlist)
{
	global $dbCore, $_SWIFT;

	$_finalticketfilteridlist = array();

	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."ticketfilters` WHERE `ticketfilterid` IN (". buildIN($ticketfilteridlist) .");");
	while ($dbCore->nextRecord())
	{
		if ($dbCore->Record["filtertype"] == SWIFTPUBLIC || ($dbCore->Record["filtertype"] == SWIFTPRIVATE && $dbCore->Record["staffid"] == $_SWIFT["staff"]["staffid"]))
		{
			$_finalticketfilteridlist[] =$dbCore->Record["ticketfilterid"];
		}
	}

	if (!_is_array($_finalticketfilteridlist))
	{
		return false;
	}

	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."ticketfilters` WHERE `ticketfilterid` IN (". buildIN($_finalticketfilteridlist) .");");
	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."ticketfilterfields` WHERE `ticketfilterid` IN (". buildIN($_finalticketfilteridlist) .");");

	rebuildFilterCache();

	return true;
}

/**
* Insert a new Filter
*/
function insertTicketFilter($title, $filtertype, $searchrules, $ruleoptions)
{
	global $dbCore, $_SWIFT;

	if (!$filtertype)
	{
		$filtertype = SWIFTPRIVATE;
	}

	$dbCore->query("INSERT INTO `". TABLE_PREFIX ."ticketfilters` (`dateline`, `filtertype`, `title`, `staffid`, `filteroptions`) VALUES ('". DATENOW ."', '". $dbCore->escape($filtertype) ."', '". $dbCore->escape($title) ."', '". intval($_SWIFT["staff"]["staffid"]) ."', '". $dbCore->escape($ruleoptions) ."');");
	$_ticketfilterid = $dbCore->lastInsertId();

	if (empty($_ticketfilterid))
	{
		return false;
	}

	foreach ($searchrules as $key=>$val)
	{
		$dbCore->query("INSERT INTO `". TABLE_PREFIX ."ticketfilterfields` (`ticketfilterid`, `dateline`, `fieldtitle`, `fieldoper`, `fieldvalue`) VALUES ('". intval($_ticketfilterid) ."', '". DATENOW ."', '". $dbCore->escape($val[0]) ."', '". intval($val[1]) ."', '". $dbCore->escape($val[2]) ."');");
	}

	rebuildFilterCache();

	return $_ticketfilterid;
}

			updateTicketFilter($_ticketfilter["ticketfilterid"], $_POST["title"], $_POST["filtertype"], $_POST["searchrules"], $_POST["ruleoptions"]);


/**
* Update existing Filter
*/
function updateTicketFilter($ticketfilterid, $title, $filtertype, $searchrules, $ruleoptions)
{
	global $dbCore, $_SWIFT;

	if (empty($ticketfilterid))
	{
		return false;
	}

	if (!$filtertype)
	{
		$filtertype = SWIFTPRIVATE;
	}

	$dbCore->query("UPDATE `". TABLE_PREFIX ."ticketfilters` SET `dateline` = '". DATENOW ."', `filtertype` = '". $dbCore->escape($filtertype) ."', `title` = '". $dbCore->escape($title) ."', `staffid` = '". intval($_SWIFT["staff"]["staffid"]) ."', `filteroptions` = '". $dbCore->escape($ruleoptions) ."' WHERE `ticketfilterid` = '". intval($ticketfilterid) ."';");

	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."ticketfilterfields` WHERE `ticketfilterid` = '". intval($ticketfilterid) ."';");

	foreach ($searchrules as $key=>$val)
	{
		$dbCore->query("INSERT INTO `". TABLE_PREFIX ."ticketfilterfields` (`ticketfilterid`, `dateline`, `fieldtitle`, `fieldoper`, `fieldvalue`) VALUES ('". intval($ticketfilterid) ."', '". DATENOW ."', '". $dbCore->escape($val[0]) ."', '". intval($val[1]) ."', '". $dbCore->escape($val[2]) ."');");
	}

	rebuildFilterCache();

	return $_ticketfilterid;
}

/**
* Convert Array to JS to Render the Criteria
*/
function criteriaArrayToForm($_criteria)
{
	foreach ($_criteria as $key=>$val)
	{
		$jscode .= 'newRuleCriteria(\''. addslashes($val["fieldtitle"]) .'\', \''. intval($val["fieldoper"]) .'\', \''. addslashes($val["fieldvalue"]) .'\');'.SWIFT_CRLF;
	}

	return $jscode;
}
?>